// import axios from "axios";//为了使用all
import $axios from "@/common/http.js";//请求实例
import { Message } from "element-ui";
import router from "@/router"

//菜单列表
export const getMenuList = async (id) => {
    let rst
    if (id) {
        rst = await $axios.get("/menuinfo?id=" + id)
    } else
        rst = await $axios.get("/menulist?istree=1")
    return rst

}

/**
 * 添加菜单
 * @param {*} data  提交给服务器的数据
 * @returns 
 */
export const addMenu = async data => {
    // console.log(data);
    let rst = await $axios.post("/menuadd", data)
    // console.log(rst);
    if (rst.code == 200)
        Message({
            type: "success",
            message: "菜单添加成功",
            duration: 500,
            onClose() {//关闭之后，回到页面
                router.go(-1);
            }
        })
    // console.log('结果：',rst);
    // return rst;
}


//编辑菜单
/**
 * 
 * @param {*} data 
 * @param {*} id 
 * @returns 
 */
export const editMenu = async (data, id) => {
    // console.log(id);
    let rst = await $axios.post('/menuedit', { id, ...data })
    // return rst
    if (rst.code == 200) {
        Message({
            type: "success",
            message: rst.msg,
            duration: 500,
            onClose() {//关闭之后，回到页面
                router.go(-1);
            }
        })
    }
}


export const delMenu = async (id) => {
    let rst = await $axios.post('/menudelete', { id })
    // console.log(rst);
    if (rst.code == 200) {
        Message({
            type: "success",
            message: rst.msg,
            duration: 500
        })
        return true
    } else
        return false

}
